from rest_framework import serializers

from datasource.models import OpList
from dvadmin.utils.serializers import CustomModelSerializer
from dvadmin.utils.viewset import CustomModelViewSet


class OpSerializer(CustomModelSerializer):
    class Meta:
        model = OpList
        fields = "__all__"

    def create(self, validated_data):
        return super().create(validated_data)


class OpViewSet(CustomModelViewSet):
    """
    算子管理接口
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    queryset = OpList.objects.all()

    def get_queryset(self):
        type_id = self.request.query_params.get('typeid')
        print(type_id)
        if type_id is None:
            self.queryset = OpList.objects.all()
        else:
            self.queryset = OpList.objects.filter(type_id=type_id)
        return self.queryset

    def get_querycode(self, name):
        print(name)
        if name is None:
            return OpList.objects.all()
        else:
            return OpList.objects.filter(name=name).first()

    serializer_class = OpSerializer
    filter_fields = ['name', ]
    permission_classes = []
